<?php
// 获取商品列表
// header("content-type:text/html;charset=utf-8");
include_once("conn.php");

//获取所有商品列表SQL
if (empty($_GET['s_way']) || $_GET['s_way'] == 'all') {
    $sql = "SELECT * FROM tb_goods_info WHERE g_is_sell = 1 ORDER BY g_sell_num DESC";
}
//获取蔬菜类商品列表SQL
else if ($_GET['s_way'] == 'vegetable') {
    $sql = "SELECT * FROM tb_goods_info WHERE g_type='蔬菜' AND g_is_sell = 1 ORDER BY g_sell_num DESC";
}
//获取水果类商品列表SQL
else if ($_GET['s_way'] == 'fruit') {
    $sql = "SELECT * FROM tb_goods_info WHERE g_type='水果' AND g_is_sell = 1 ORDER BY g_sell_num DESC";
}
//获取海鲜类商品列表SQL
else if ($_GET['s_way'] == 'seafood') {
    $sql = "SELECT * FROM tb_goods_info WHERE g_type='生鲜' AND g_is_sell = 1 ORDER BY g_sell_num DESC";
}
//获取粮油类商品列表SQL
else if ($_GET['s_way'] == 'grain') {
    $sql = "SELECT * FROM tb_goods_info WHERE g_type='粮油' AND g_is_sell = 1 ORDER BY g_sell_num DESC";
}
//通过商品名称模糊查询
else if ($_GET['s_way'] == 'name') {
    $name = $_GET['name'];
    $sql = "SELECT * FROM tb_goods_info WHERE g_name LIKE '%" . $name . "%' AND g_is_sell = 1 ORDER BY g_sell_num DESC";
}

//执行SQL
$result = mysqli_query($conn, $sql);

if ($result) {
    if (mysqli_num_rows($result) > 0) {
        $c = 0;
        while ($row = mysqli_fetch_assoc($result)) {
            if ($c % 5 == 0) {
                echo "<div class='goods_list_row'>";
            }
            echo "
                <div class='goods_list_items'>
                    <a href='u_show_goods.php?g_id=" . $row['g_id'] . "'>
                        <img src='../goods_imgs/" . $row['g_img'] . "'>
                        <p class='p'>" . $row['g_name'] . "</p>
                        <p>￥" . $row['g_price'] . "</p>
                    </a>
                </div>
            ";
            if ($c % 5 == 4) {
                echo "</div>";
            }
            $c++;
        }
    } else {
        echo "
            <div class='null_goods'>
                <img src='img/null_goods.svg'>
            </div>
        ";
    }
}